﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorldConqueror.Libraries.Maths.Costants;
using WorldConqueror.Libraries.Maths.MathShapes;

namespace WorldConqueror.Libraries.Maths.Functions.Distance
{
    public class HaversineFormula : DistanceBetweenCoordinates
    {
        public HaversineFormula(IMathCoordinate parameter1, IMathCoordinate parameter2)
            : base(parameter1, parameter2)
        { }

        public override double CalculateBase(
            double radiansLatitude1, double radiansLongitude1,
            double radiansLatitude2, double radiansLongitude2)
        {
            var latitudeDifference = radiansLatitude2 - radiansLatitude1;
            var longitudeDifference = radiansLongitude2 - radiansLongitude1;

            var a =
                Math.Pow(Math.Sin(latitudeDifference / 2), 2) +
                Math.Pow(Math.Sin(longitudeDifference / 2), 2) *
                Math.Cos(radiansLatitude1) * Math.Cos(radiansLatitude2);

            var distanceIncrementFactor = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            var distance = EarthConstants.EarthRadius * distanceIncrementFactor;

            return distance;
        }
    }
}
